package com.applicationLayer.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.applicationLayer.formbean.CommentForm;
import com.applicationLayer.formbean.CovoitureurForm;
import com.applicationLayer.sessionbean.SessionBean;
import com.common.domainService.ICommentService;
import com.common.domainService.ICovoitureurService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CovoitureurAction extends ActionSupport {

	private static final long serialVersionUID = 4596769309036500015L;

	Logger logger = Logger.getLogger(CovoitureurAction.class);
	
	private ICovoitureurService covoitureurDomainService;
	private ICommentService commentDomainService;
	private CommentForm comment;
	private List<CommentForm> commentList;
	private CovoitureurForm covoitureur;
	private List<String> satisfactionList;

	public String covoitureurDetails(){
		setSatisfactionList(new ArrayList<String>(){
			private static final long serialVersionUID = -1096961232466601972L;
			{
				add(getText("covoit.comment.level0"));
				add(getText("covoit.comment.level1"));
				add(getText("covoit.comment.level2"));
				add(getText("covoit.comment.level3"));
			}
		});
		final String id = (String) SessionBean.getFromSession("id_kept_covoit");
		if(id!=null){
			ActionContext.getContext().setParameters(new HashMap<String, Object>(){
				private static final long serialVersionUID = 3355936155705796222L;
				{put("id_covoitureur", id);}
			});
		}
		Object paramObj = ActionContext.getContext().getParameters().get("id_covoitureur");  
        if(paramObj != null){
        	String idC;
        	try{
        		idC=((String[])paramObj)[0];
        	}
        	catch(Exception e){
        		idC=String.valueOf(paramObj);
        	}
        	try{
        		int id_covoitureur = Integer.parseInt(idC);
        		SessionBean.setInSession("satisfactionList", satisfactionList);
        		commentList = commentDomainService.getCovoitureurComments(id_covoitureur);
        		SessionBean.setInSession("commentList", commentList);
        		covoitureur = covoitureurDomainService.getById(id_covoitureur);
            	if(covoitureur==null)return "HOME";
            	SessionBean.setInSession("covoitureurDetails", covoitureur);
        	}
        	catch(NumberFormatException e){
        		return "HOME";
        	}
        }
        else{
        	logger.info("covoitureurDetails() undone : path id param is missing");
        	return "HOME";
        }
        logger.info("covoitureurDetails() done");
		return "SUCCESS";
	}
	
	public String postComment(){
		if(!SessionBean.checkUserInSession()){
			return "SUCCESS";
		}
		satisfactionList = (List<String>) SessionBean.getFromSession("satisfactionList");
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		covoitureur = (CovoitureurForm) SessionBean.getFromSession("covoitureurDetails");
		commentList = (List<CommentForm>) SessionBean.getFromSession("commentList");
		if(!validateField())
			return "COMMENT";
		commentDomainService.addCovoitureurComment(comment,co.getId_covoitureur(),covoitureur.getId_covoitureur());
		SessionBean.setInSession("id_kept_covoit",String.valueOf(covoitureur.getId_covoitureur()));
		return "SUCCESS";
	}
	
	public boolean validateField(){
		boolean isValid=true;
		if(comment!=null){
			if (comment.getComment().length()==0){	
				addFieldError( "comment.comment", " Content is required" );
				isValid=false;
			}
			if (comment.getTitle().length()==0){	
				addFieldError( "comment.title", " Title is required" );
				isValid=false;
			}
		}
		logger.info("validate() done");
		return isValid;
	}
	public boolean isCovoitureurOnDetails(){
		if(!SessionBean.checkUserInSession()){
			return false;
		}
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		CovoitureurForm co_details = (CovoitureurForm) SessionBean.getFromSession("covoitureurDetails");
		if(co_details!=null){
			return co_details.getId_covoitureur()==co.getId_covoitureur();
		}
		return false;
	}
	
	/** GETTER **/
	
	public ICovoitureurService getCovoitureurDomainService() {
		return covoitureurDomainService;
	}
	
	public CovoitureurForm getCovoitureur() {
		return covoitureur;
	}
	
	public ICommentService getCommentDomainService() {
		return commentDomainService;
	}

	public List<CommentForm> getCommentList() {
		return commentList;
	}

	public List<String> getSatisfactionList() {
		return satisfactionList;
	}
	
	public CommentForm getComment() {
		return comment;
	}

	/** SETTER **/

	public void setCovoitureurDomainService(
			ICovoitureurService covoitureurDomainService) {
		this.covoitureurDomainService = covoitureurDomainService;
	}

	public void setCovoitureur(CovoitureurForm covoitureur) {
		this.covoitureur = covoitureur;
	}
	
	public void setCommentDomainService(ICommentService commentDomainService) {
		this.commentDomainService = commentDomainService;
	}
	
	public void setCommentList(List<CommentForm> commentList) {
		this.commentList = commentList;
	}

	public void setSatisfactionList(List<String> satisfactionList) {
		this.satisfactionList = satisfactionList;
	}
	
	public void setComment(CommentForm comment) {
		this.comment = comment;
	}
}